%%% Calculates reversal, a strong form of choice inconsistency which
%%% captures (how often do people switch back to 
%%% a safer/nearer option after having already picked the riskier/more
%%% distant option in a given choice group 

% The algorithm for estimating the reversal index is as follows: get the
% column position of all the "1"s i.e. instances in which the 2nd option is chosen
% (more distant/more risky). If there are gaps in the indices(the distance
% between two "1"s is larger than 1), a reversal has logically taken
% place.

%%% In time choice tasks, choice groups are ALWAYS of size 6
time_reversal_n=NaN(ind_n,1);
time_reversal_n1=NaN(ind_n,1);
time_reversal_n2=NaN(ind_n,1);
rev_last=zeros(ind_n,1);
choices=temporalchoice;
time_first_ones=NaN(ind_n,8);

for i=1:ind_n
    temp1=choices(i,:);
    reversal_n_i=0;
    for j=1:8
        temp=temp1(1:6);
        %%% generates a position index of the "1"s (riskier/more distant
        %%% choices) 
        index=find(temp==1);
        %%% Tracks the first instance of picking the riskier/more distant
        %%% option in each choice group (this is not a reversal, just the
        %%% "tipping" point after which we would expect only risky/distant
        %%% choices for the choice group).
        first_one=min(index);
        if isempty(index)==0
            %%% Generates a lag of the position index - lets one get the
            %%% gap between successive "1"s
            index_L1=[NaN index(1:end-1)];
            index_diff=index-index_L1;
            reversal_n_i=reversal_n_i+sum(index_diff>1);
            %%% the last position needs to get special treatment as
            %%% there is no next "1" to compare to. If at least one "1" was
            %%% chosen by an individual in a given choice group, and there
            %%% is a "0" at the end, then at least one reversal has not
            %%% been recorded (as the last chosen "1" has no next "1" to
            %%% compare its index position to)     
            if max(index)~=6
                reversal_n_i=reversal_n_i+1;
                rev_last(i)=rev_last(i)+1;
            end
            time_first_ones(i,j)=first_one;
            clear index first_one index_L1 index_diff
        end
        temp1(1:6)=[];
        %%% gets reversals separately by time-horizon (G1-4 v G5-8)
        if j==4
            time_reversal_n1(i)=reversal_n_i;
        end
        if j==8
            time_reversal_n2(i)=reversal_n_i-time_reversal_n1(i);
        end        
    end
    time_reversal_n(i)=reversal_n_i;
    clear reversal_n_i    
end


%%% For lottery choices this is more complicated. There are first 3 groups
%%% of 10 questions which should behave as the time questions - as one goes
%%% further down in each group, the riskier option becomes more attractive
%%% and thus more and more individuals should switch to it. The remaining
%%% questions are organized in 5 groups of 5. As one goes down each group, the riskier option becomes
%%% LESS attractive.

%%% Tracks reversals in the 1st set of choice groups where the riskier
%%% option becomes progressively more attractive
risk_reversal_n1=NaN(ind_n,1);
%%% Tracks reversals in the 2nd set of choice groups where the riskier
%%% option becomes progressively less attractive
risk_reversal_n2=NaN(ind_n,1);
risk_first_ones=NaN(ind_n,8);

rev_last=zeros(ind_n,1);
choices=lotterychoice;

%%% First set takes care of the three 10 question groups
for i=1:ind_n
    temp1=choices(i,:);
    reversal_n_i=0;
    for j=1:3
        temp=temp1(1:10);
        index=find(temp==1);
        first_one=min(index);
        if isempty(index)==0
            index_L1=[NaN index(1:end-1)];
            index_diff=index-index_L1;
            reversal_n_i=reversal_n_i+sum(index_diff>1);
            if max(index)~=10
                reversal_n_i=reversal_n_i+1;
                rev_last(i)=rev_last(i)+1;
            end
            risk_first_ones(i,j)=first_one;            
            clear index first_one index_L1 index_diff
        end
        temp1(1:10)=[];
    end
    risk_reversal_n1(i)=reversal_n_i;
    clear reversal_n_i    
end

%%% Second set takes care of the five 5 question groups. As now through the
%%% group one expects to see a switch to 0s from 1s, a reversal would be
%%% the opposite of above - so set find function to find 0s. 

for i=1:ind_n
    temp1=choices(i,31:55);
    reversal_n_i=0;
    for j=1:5
        temp=temp1(1:5);
        index=find(temp==0);
        first_one=min(index);
        if isempty(index)==0
            index_L1=[NaN index(1:end-1)];
            index_diff=index-index_L1;
            reversal_n_i=reversal_n_i+sum(index_diff>1);
            if max(index)~=5
                reversal_n_i=reversal_n_i+1;
                rev_last(i)=rev_last(i)+1;
            end
            risk_first_ones(i,j+3)=first_one;
            clear index first_one index_L1 index_diff
        end
        temp1(1:5)=[];
    end
    risk_reversal_n2(i)=reversal_n_i;
    
    clear reversal_n_i    
end

risk_reversal_n=risk_reversal_n1+risk_reversal_n2;

reversal_n=risk_reversal_n+time_reversal_n;